package com.example.yiwei.crm.view.activity.clueActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.clueDao;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.dao_impl.clueDaoImpl;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.data.GetClue;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.ClueMeesageEntity;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.view.activity.contactActivity.ContactDetaledMessageActivity;
import com.example.yiwei.crm.view.activity.contactActivity.ContactSearchActivity;
import com.example.yiwei.crm.view.activity.customerActivity.CustomerDetailedMessageActivity;
import com.example.yiwei.crm.view.activity.customerActivity.CustomerSearchActivity;

import java.util.List;

public class ClueSearchMessageActivity extends AppCompatActivity {
    private EditText edit_search;
    private ListView listview;
    private Button btn_search;
    MyAdapter adpter;
    boolean tag = false;
    List<ClueMeesageEntity> clueList;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clue_search_message);
        listview= (ListView) findViewById(R.id.clue_search_listview_search);
        edit_search= (EditText) findViewById(R.id.clue_search_edit_search);
        btn_search= (Button) findViewById(R.id.clue_search_btn_search);
        adpter=new MyAdapter();
        listview.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        listview.setDividerHeight(1);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final clueDao clueDao=new clueDaoImpl();
                        if ("xiansuochi".equals(getIntent().getStringExtra("xiansuochiTag"))){
                            clueList=clueDao.querySimpleClueMessage(baseClass.clue_group_search_url+edit_search.getText().toString(),null);
                        }else{
                            clueList=clueDao.querySimpleClueMessage(baseClass.clue_search_url+edit_search.getText().toString(),null);
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (clueList.size()==0){
                                    Toast.makeText(ClueSearchMessageActivity.this, "联系人不存在!", Toast.LENGTH_SHORT).show();
                                }
                                listview.setAdapter(adpter);
                                adpter.notifyDataSetChanged();
                                listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                        final int clue_id=clueList.get(i).getClue_id();
                                        intent=new Intent(ClueSearchMessageActivity.this,ClueDetailedMessageActivity.class);
                                        intent.putExtra("id",clue_id);
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                clueDao clueDao1=new clueDaoImpl();
                                                GetClue.clue= clueDao1.queryDetailedClueMessage(baseClass.query_detailed_clue_message_url+clue_id, null);
                                                if ("success".equals(GetClue.clue.getTag())){
                                                    startActivity(intent);
                                                    finish();
                                                }else{
                                                    runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            Toast.makeText(ClueSearchMessageActivity.this, "数据加载失败", Toast.LENGTH_SHORT).show();
                                                        }
                                                    });
                                                }

                                            }
                                        }).start();
                                    }
                                });

                            }
                        });
                    }
                }).start();
            }
        });

    }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return clueList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.listview_item,null);
                holder = new ViewHolder();
                holder.item_text1 = (TextView) convertView.findViewById(R.id.listview_item_txt_name);
//                holder.item_text2= (TextView) convertView.findViewById(R.id.listview_item_txt_bz);
                holder.item_text3 = (TextView) convertView.findViewById(R.id.listview_item_createTime);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.item_text1.setText(clueList.get(position).getClue_name());
            holder.item_text3.setText(clueList.get(position).getClue_mobile_phone());
//            if (customerList.get(position).getCreate_time()!=null){
//                SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
//                sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//                String time = customerList.get(customerList.size()-1-position).getCreate_time();
//                long l = Long.parseLong(time);
//                Date date = new Date(l);
//                String create_time = sdf.format(date);
//                holder.item_text3.setText(create_time);
//            }
            return convertView;
        }
        class ViewHolder {
            TextView item_text1, item_text2, item_text3,item_text4;
        }
    }
}
